﻿using Dao;
using Dao.Impl;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dao.Impl
{
    /// <summary>
    /// 命令的数据库层
    /// </summary>
    public class CommandDaoImpl: BaseDaoImpl<string, Command>, CommandDao
    {
        /// <summary>
        /// 命令的数据存储 id, Command
        /// </summary>
        public List<Command> Commands = new List<Command>();

        public override int Delete(string m)
        {
            return 0;
        }

        public override int Save(Command t)
        {
            Commands.Add(t);
            return 1;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="t">name, view</param>
        /// <returns></returns>
        public override List<Command> Search(Command t)
        {
            var result = Commands.Select(c=>c);
            if (!string.IsNullOrEmpty(t.Name))
            {
                result = result.Where(p => p.Name.StartsWith(t.Name));
            }
            if (t.View != null && t.View.Count >= 0)
            {
                result = result.Where(p =>
                {
                    foreach(string v in p.View)
                    {
                        if (t.View.Contains(v))
                        {
                            return true;
                        }
                    }
                    return false;
                });
            }
            return result.ToList<Command>();
        }

        public List<Command> SearchByCommandName(string rootCmd)
        {
            return Commands.Where(c => c.Name.StartsWith(rootCmd)).ToList();
        }

        public List<Command> SearchByCommandNameAndView(string rootCmd, string view)
        {
            return Commands.Where(c => {
                if(c.View != null && c.View.Count > 0)
                {
                    return c.Name.StartsWith(rootCmd) && c.View.Contains(view);
                }
                else
                {
                    return c.Name.StartsWith(rootCmd);
                }
            }).ToList();
        }

        public override Command SearchByPrimaryKey(string m)
        {
            var command = Commands.Where(c => c.Name.Equals(m)).First();
            return command;
        }

        public override Command SearchOne(Command t)
        {
            List<Command> c = Search(t);
            if(c.Count >= 1)
            {
                return c[0];
            }
            return null;
        }

        public override int Update(Command t)
        {
            return 0;
        }
    }
}
